// The module 'vscode' contains the VS Code extensibility API
// Import the module and reference it with the alias vscode in your code below
import * as vscode from 'vscode';
import * as path from 'path';
import * as fs from 'fs';
import { ROS2ExecutableProvider } from './resourceTree';
import { RegeisterCommands } from './command';
import { MonitorProvider } from './monitorTree';
import { EntendToolsProvider as ToolsProvider } from './extendToolsTree';

let resourceTree: ROS2ExecutableProvider;
let symlinkBarItem: vscode.StatusBarItem;
let buildAllBarItem: vscode.StatusBarItem;
let buildLastBarItem: vscode.StatusBarItem;
let runLastBarItem: vscode.StatusBarItem;
export function activate(context: vscode.ExtensionContext) {
	const rootPath =
		vscode.workspace.workspaceFolders && vscode.workspace.workspaceFolders.length > 0
			? vscode.workspace.workspaceFolders[0].uri.fsPath
			: undefined;
	if (!rootPath) {
		vscode.window.showErrorMessage('No workspace folder found.');
		return;
	}

	symlinkBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
	symlinkBarItem.command = 'ros2-helper.change_symlink_install';
	updateSymlinkBarItem();
	symlinkBarItem.show();
	context.subscriptions.push(symlinkBarItem);
	context.subscriptions.push(
		vscode.workspace.onDidChangeConfiguration(event => {
			if (event.affectsConfiguration('ros2-helper.use_symlink_install')) {
				updateSymlinkBarItem();
			}
		})
	);

	buildAllBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
	buildAllBarItem.command = 'ros2-helper.build_all';
	buildAllBarItem.text = '$(run-view-icon) build all'
	buildAllBarItem.tooltip = 'build all packages in workspace';
	buildAllBarItem.show();
	context.subscriptions.push(buildAllBarItem);

	buildLastBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
	buildLastBarItem.command = 'ros2-helper.build_last';
	buildLastBarItem.text = '$(run-view-icon) build last'
	buildLastBarItem.tooltip = 'build last target';
	buildLastBarItem.show();
	context.subscriptions.push(buildLastBarItem);

	runLastBarItem = vscode.window.createStatusBarItem(vscode.StatusBarAlignment.Left);
	runLastBarItem.command = 'ros2-helper.run_last';
	runLastBarItem.text = '$(run) run last'
	runLastBarItem.tooltip = 'run last target';
	runLastBarItem.show();
	context.subscriptions.push(runLastBarItem);



	resourceTree = new ROS2ExecutableProvider(rootPath, context);
	vscode.window.registerTreeDataProvider('resource', resourceTree);
	let monitorTree = new MonitorProvider();
	vscode.window.registerTreeDataProvider('monitor', monitorTree);
	let toolsTree = new ToolsProvider();
	vscode.window.registerTreeDataProvider('tools', toolsTree);

	RegeisterCommands(context, resourceTree, monitorTree, toolsTree);
}

function updateSymlinkBarItem() {
	const cfg = vscode.workspace.getConfiguration();
	let enable = cfg.get<boolean>('ros2-helper.use_symlink_install');
	if (enable) {
		symlinkBarItem.text = '$(explorer-view-icon) Symlink';
		symlinkBarItem.tooltip = 'Copy files when installing';
	} else {
		symlinkBarItem.text = '$(source-control-view-icon) Copy';
		symlinkBarItem.tooltip = 'Create symbolic links when installing';
	}
}